﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Auditoria
{
    public partial class Logs : Form
    {
        Conexion conn;
        Principal principal2;


        public void getPrincipal2(Principal log)
        {
            this.principal2 = log;
        }
        public Logs(Conexion conexion)
        {
            InitializeComponent();
            this.conn = conexion;
            this.nose();
        }
        public void nose()
        {

            this.dataGridView1.DataSource = null;

            List<String> lista = new List<String>();
            lista = this.conn.todos_logs_offline();
            DataTable dt = new DataTable();
            dt.Columns.Add("Secuencia");
            dt.Columns.Add("Log");
            dt.Columns.Add("Fecha");
            DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
            chk.Name = "Cargar";
            dataGridView1.Columns.Add(chk);
            if (lista.Count == 1)
                return;
            for (int i = 1; i < lista.Count; i = i + 3)
            {
                DataRow row = dt.NewRow();
                row["Secuencia"] = lista[i];
                row["Log"] = lista[i + 1];
                row["Fecha"] = lista[i + 2];
                dt.Rows.Add(row);
            }
            this.dataGridView1.DataSource = dt;
            this.dataGridView1.Update();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = null;

            List<String> lista = new List<String>();
            lista = this.conn.todos_logs_offline();
            DataTable dt = new DataTable();
            dt.Columns.Add("Secuencia");
            dt.Columns.Add("Log");
            dt.Columns.Add("Fecha");
            DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
            chk.Name = "Cargar";
            //dataGridView1.Columns.Add(chk);
            if (lista.Count == 1)
                return;
            for (int i = 1; i < lista.Count; i = i + 3)
            {
                string fecha = lista[i + 2];
                DateTime MyDateTime;
                MyDateTime = new DateTime();
                MyDateTime = DateTime.Parse(fecha);
                if (MyDateTime.CompareTo(dtp_Desde.Value) >= 0 && MyDateTime.CompareTo(dtp_Hasta.Value) < 0)
                {
                    DataRow row = dt.NewRow();
                    row["Secuencia"] = lista[i];
                    row["Log"] = lista[i + 1];
                    row["Fecha"] = lista[i + 2];
                    dt.Rows.Add(row);
                }
            }
            this.dataGridView1.DataSource = dt;
            this.dataGridView1.Update();
        }

        private void atrasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            this.principal2.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            this.principal2.setLogs(this.Lista());
            this.Close();
            this.principal2.Show();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {

                DataGridViewCheckBoxCell chkCelda = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];

                chkCelda.Value = true;

            }
        }
        private List<String> Lista()
        {
            List<String> logs = new List<String>(); ;
            for (int i = 0; i < dataGridView1.RowCount-1; i++)
            {
                DataGridViewCheckBoxCell chkCelda = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
                if (Convert.ToBoolean(chkCelda.Value) == true)
                {
                    logs.Add(dataGridView1.Rows[i].Cells[2].Value.ToString());
                }
            }
            return logs;
        }
        

    }
}
